package DB

import (
	"bilichatroom/pkg/util/gopath"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
	"github.com/xormplus/xorm"
)

var (
	dataSource string
	Engine     *xorm.Engine
)

func Init() {
	dbConfig := MySQLConfig{
		Host:    "127.0.0.1",
		Port:    3306,
		User:    "root",
		Pswd:    "12345678",
		Dbname:  "chatroom",
		Charset: "utf8mb4",
	}
	dataSource = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s",
		dbConfig.User,
		dbConfig.Pswd,
		dbConfig.Host,
		dbConfig.Port,
		dbConfig.Dbname,
		dbConfig.Charset,
	)

	var err error
	if Engine, err = xorm.NewEngine("mysql", dataSource); err != nil {
		panic(err)
	}
	Engine.ShowSQL(false)

	filePath := gopath.FindFilePath("sqlmap")
	if err = Engine.RegisterSqlTemplate(xorm.Pongo2(filePath, ".stpl")); err != nil {
		panic(err)
	}
}
